<?php

/**
 * This is the model class for table "modulo".
 *
 * The followings are the available columns in table 'modulo':
 * @property integer $id
 * @property string $nombre
 *
 * The followings are the available model relations:
 * @property Menu[] $menus
 */
class Modulo extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Modulo the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'modulo';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nombre', 'required'),
			array('nombre', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, nombre', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'menus' => array(self::MANY_MANY, 'Menu', 'modulo_menu(id_modulo, id_menu)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nombre' => 'Nombre',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('nombre',$this->nombre,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function guardarModulo($datos){
		$id_modulo=-1;
		$modulo=new Modulo();
		$modulo->attributes=$datos;
		$modulo->save();
		$id_modulo=$modulo->id;
		return $id_modulo;
	}
	
	public function buscarModuloPaginado($indiceRegistroInicio, $numeroRegistrosPagina,$busqueda){
		$busqueda=addcslashes($busqueda,'%_');
		$criteria=new CDbCriteria(array(
				'condition'=>"nombre like :busqueda",
				'params'=>array(':busqueda'=>"%$busqueda%"),				
		));
		$resultados=Yii::app()->paginacionUtil->buscarRegistrosPaginados($indiceRegistroInicio,$numeroRegistrosPagina,$criteria,Modulo::model());
		return $resultados;
	}
	
	public function buscarModulosPaginadoArray($indiceRegistroInicio, $numeroRegistrosPagina,$busqueda){
		$modulos=$this->buscarModuloPaginado($indiceRegistroInicio, $numeroRegistrosPagina, $busqueda);
		$modulosAArray=array();
		foreach ($modulos as $modulo){
			$moduloAArray=array();
			$moduloAArray['id']=$modulo->id;
			$moduloAArray['nombre']=$modulo->nombre;
			$moduloAArray['identificador']=$modulo->identificador;
			$modulosAArray[]=$moduloAArray;
		}
		return $modulosAArray;
	}
	
	public function actualizarDatosModulo($datos){
		Modulo::model()->updateByPk($datos['id'],$datos);	
		return true;
	}    
    
    public function obtenerMenus($id){
    	$sql="select m.* from menu m
    			 inner join modulo_menu mm on mm.id_menu=m.id
    			 inner join modulo modu on modu.id=mm.id_modulo
    			 where modu.id=:id_modulo and m.nivel=0 order by m.indice asc";
    	$connection=Yii::app()->db;
    	$command=$connection->createCommand($sql);
    	$command->bindParam(':id_modulo',$id,PDO::PARAM_INT);
    	$rows=$command->queryAll();
    	return $rows;
    }
    
    public function obtenerMenusAR($identificador){
    	$criteriaBusquedaHijos=new CDbCriteria(array(
    			'condition'=>"modu.identificador=:identificador",
    			'params'=>array(':identificador'=>$identificador),
    			'alias'=>'m',
    			'join'=>'inner join modulo_menu mm on mm.id_menu=m.id
    					 inner join modulo modu on modu.id=mm.id_modulo')
    	);
    	return Menu::model()->findAll($criteriaBusquedaHijos);
    }    
    
    
}